package com.example.demo.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2023-08-05
 * Time: 8:43
 */
@RestController
@RequestMapping("/hhh")
public class UserController {
    @RequestMapping("/upfile")
    public String upload(@RequestPart("myfile")MultipartFile file) throws IOException {
         //生成唯一id,采用UUID
        String name= UUID.randomUUID().toString().replace("-","");
        //得到源文件后缀名
        name+=(file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")));
        String path="D:\\image\\"+name;
           //保存文件
            file.transferTo(new File(path));
            return path;
    }
    @RequestMapping("/getcookie")
    public String getcookie(@CookieValue(value = "java",required = false) String cookie){
                return cookie;
    }

    public static  String Sessionkey="session";
    @RequestMapping("/setsession")
    public String setsession(HttpServletRequest request){
        HttpSession session=request.getSession();
        if(session!=null){
            session.setAttribute("Sessionkey","王麻子");
            return "success";
        }else{
            return "fail";
        }

    }
    @RequestMapping("/getsession")
    public String getSessionkey(@SessionAttribute(value="Sessionkey",required = false)String session){
                return session;
    }
    @RequestMapping("/header")
    public String getheader(@RequestHeader("User-Agent") String userAgent){
        return "userAgent"+userAgent;
    }

    @RequestMapping("/introduction")
    public String getName(){
      return "老师好我叫魏鸿雁!" ;

    }

}
